package com.pine.app.module.security.oauth.provider.authrizationCode;

import com.pine.app.module.security.oauth.provider.OAuth2Authentication;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

/**
 * @author xiaoyuan
 * @create 2019/10/18 12:13
 **/

public class AuthenticationCodeServiceImpl implements AuthenticationCodeService {

    private AuthenticationCodeStore codeStore;

    @Autowired
    public void setCodeStore(AuthenticationCodeStore codeStore) {
        this.codeStore = codeStore;
    }

    public AuthenticationCodeServiceImpl(){

    }

    public AuthenticationCodeServiceImpl(AuthenticationCodeStore codeStore){
        this.codeStore = codeStore;
    }

    @Override
    public String createCode(OAuth2Authentication authentication) {
        String code = "P-" + UUID.randomUUID().toString().replaceAll("-","");
        AuthenticationCode oauthCode = new AuthenticationCode();
        oauthCode.setCode(code);
        oauthCode.setOAuth2Authentication(authentication);
        codeStore.storeCode(oauthCode);
        return code;
    }

    @Override
    public AuthenticationCode getByCode(String code) {
        return codeStore.getByCode(code);
    }

    @Override
    public void removeByCode(String code) {
        codeStore.removeByCode(code);
    }
}
